﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Net.BLL;
using UcAsp.Net.Model;
using System.IO;
namespace UcAsp.Net.Web.Controllers
{
    public class ShowMessageController : Controller
    {
        //
        // GET: /ShowMessage/

        public ActionResult Index(int id, int? pageindex)
        {
            SettingConfig.Theme(Request.UserAgent);
            int artid = id;

            ArticelsViews art = new ArticelsViews();
            int count;
            List<Articles> list;
            art = new ArticlesBLL().GetArtieById(artid,out count,out list);
            if (art == null)
            {
               Response.Redirect("/Error/NoFound");
                return null;
            }
            ViewData["Count"] = count;
            string view = "~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/ShowMessage/";
            string cateview = "~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/Categories/";
            Categories categories = new CategoriesBLL().GetCategoriesById(art.CategorieID);
            if (!string.IsNullOrEmpty(art.ViewsTemp))
            {
                if (art.ViewsTemp.Trim() == "/")
                {
                    if (categories.ContentTempPath.Trim() == "/")
                    {
                        view = view + "index.cshtml";
                    }
                    else
                    {
                        view = view + categories.ContentTempPath + ".cshtml";
                    }
                }
                else
                {
                    view = view + art.ViewsTemp + ".cshtml";
                }
            }
            else
            {
                if (categories.ContentTempPath.Trim() == "/")
                {
                    view = view + "index.cshtml";
                }
                else
                {
                    view = view + categories.ContentTempPath + ".cshtml";
                }
            }

            ViewData["NextOrPrev"] = list;
            return View(view, art);
        }
        public ActionResult ShowPath(string showpath)
        {
            SettingConfig.Theme(Request.UserAgent);
            long s = DateTime.Now.Ticks / 10000;
            ArticelsViews art = new ArticelsViews();
            int count;
            List<ArticelsViews> list;
            Response.Write("m:" + ((DateTime.Now.Ticks / 10000) - s).ToString());
            art = new ArticlesBLL().GetArtieByShowPath(showpath,out count,out list);
            Response.Write("a:"+((DateTime.Now.Ticks / 10000) - s).ToString());
            if (art == null)
            {
                Response.Redirect("/Error/NoFound?error="+Request.Url);
                Response.End();
                return View() ;
            }
            string view = "~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/ShowMessage/";
            Categories categories = new CategoriesBLL().GetCategoriesById(art.CategorieID);
            Response.Write("b:" + ((DateTime.Now.Ticks / 10000) - s).ToString());
            if (!string.IsNullOrEmpty(art.ViewsTemp.Trim()))
            {
                if (art.ViewsTemp.Trim() == "/")
                {
                    if (categories.ContentTempPath.Trim() == "/")
                    {
                        view = view + "index.cshtml";
                    }
                    else
                    {
                        view = view + categories.ContentTempPath + ".cshtml";
                    }
                }
                else
                {
                    view = view + art.ViewsTemp + ".cshtml";
                }
            }
            else
            {
                if (categories.ContentTempPath.Trim() == "/")
                {
                    view = view + "index.cshtml";
                }
                else
                {
                    view = view + categories.ContentTempPath + ".cshtml";
                }
            }
            ViewData["NextOrPrev"] = list;
            Response.Write("c:" + ((DateTime.Now.Ticks / 10000) - s).ToString());
            long e=DateTime.Now.Ticks/10000;
            Response.Write("b:"+(e - s).ToString());
            return View(view, art);
        }


        public ActionResult PageShow(int id, int pageindex)
        {
            SettingConfig.Theme(Request.UserAgent);
            int artid = id;

            ArticlesPage page = new ArticlesPage();
            ArticelsViews art = new ArticelsViews();
            int count;
            List<Articles> list;
            art = new ArticlesBLL().GetArtieById(artid, out count, out list);
            ViewData["Count"] = count;
            page = new ArticlesPageBLL().GetArticlesPageByPageID(artid, pageindex);
            string view = "~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/ShowMessage/";
            string cateview = "~/themes/" + BLL.SettingConfig.GetSiteSetting.Theme + "/Categories/";
            Categories categories = new CategoriesBLL().GetCategoriesById(art.CategorieID);
            if (!string.IsNullOrEmpty(art.ViewsTemp))
            {
                if (art.ViewsTemp.Trim() == "/")
                {
                    if (categories.ContentTempPath.Trim() == "/")
                    {
                        view = view + "index_page.cshtml";
                    }
                    else
                    {
                        view = view + categories.ContentTempPath + "_page.cshtml";
                    }
                }
                else
                {
                    view = view + art.ViewsTemp + "_page.cshtml";
                }
            }
            else
            {
                if (categories.ContentTempPath.Trim() == "/")
                {
                    view = view + "index_page.cshtml";
                }
                else
                {
                    view = cateview + categories.ContentTempPath + "_page.cshtml";
                }
            }
            return View(view, page);
        }
    }
}
